package org.openfans.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;

public final class WebUtil {
	public static void letNoCache(HttpServletResponse response) {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
	}

	private static String getPageContent(String strUrl, String strPostRequest,
			String charsetName, int maxLength) {
		// 读取结果网页
		StringBuffer buffer = new StringBuffer();
		System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
		System.setProperty("sun.net.client.defaultReadTimeout", "5000");
		try {
			URL newUrl = new URL(strUrl);
			HttpURLConnection hConnect = (HttpURLConnection) newUrl
					.openConnection();
			// POST方式的额外数据
			if (strPostRequest.length() > 0) {
				hConnect.setDoOutput(true);
				OutputStreamWriter out = new OutputStreamWriter(hConnect
						.getOutputStream());
				out.write(strPostRequest);
				out.flush();
				out.close();
			}
			// 读取内容
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					hConnect.getInputStream(), charsetName));
			int ch;
			for (int length = 0; (ch = rd.read()) > -1
					&& (maxLength <= 0 || length < maxLength); length++)
				buffer.append((char) ch);
			rd.close();
			hConnect.disconnect();
			return buffer.toString().trim();
		} catch (Exception e) {
			// return "错误:读取网页失败！";
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 给定url获得网页的title
	 * 
	 * @param url
	 * @return
	 */
	public static String getTitle(String url) {
		final String defaultCharset = "utf-8";
		final int maxLength = 2000;
		if(url.indexOf("http://")==-1){
			url="http://"+url;
		}
		String body = getPageContent(url, "", defaultCharset, maxLength);
		String charsetName = getCharsetName(body);
		// 如果获取不到charset或者获得的charset不是默认,需重新读取(直接使用String转charset仍然不正确)
		if ("".equals(charsetName)
				|| !defaultCharset.equalsIgnoreCase(charsetName)) {
			body = getPageContent(url, "", charsetName, maxLength);
		}
		return getTitleByBody(body);
	}

	private static String getTitleByBody(String body) {
		String strTitle = "";
		int start = body.indexOf("<title>");
		int end = body.indexOf("</title>");
		if (start > -1 && end > -1) {
			strTitle = body.substring(start + 7, end);
		}
		return strTitle.trim();
	}

	private static String getCharsetName(String body) {
		String startStr = "<meta http-equiv=\"content-type\" content=\"text/html; charset=";
		int start = body.toLowerCase().indexOf(startStr);
		if (start == -1) {
			return "";
		}
		int i = 0;
		for (i = 0; i < 10; i++) {
			char c = body.charAt(start + startStr.length() + i);
			if (c == '"') {
				break;
			}
		}
		return body.substring(start + startStr.length(), start
				+ startStr.length() + i);
	}
}
